<?php
namespace app\admin\model;
use think\Model;

class Menu extends Model
{

    protected $autoWriteTimestamp = true;
    protected $type     = ['create_time'  =>  'timestamp', 'update_time' => 'timestamp'];
    protected $insert   = ['is_delete' => 1, 'status'=> 1];
    // protected $append   = ['del_text'];
    //protected $visible = ['id', 'create_time'];

    protected function initialize()
    {
        parent::initialize();
    }


    public function lists($map='', $order='ID DESC', $limit=10)
    {
        $obj = $this->where(array('is_delete' => 1))->where($map)->order($order)->paginate($limit);
        $list= $obj->toArray();
        $page= $obj->render();
        $data['list'] = $list['data'];
        $data['page'] = $page;
        return $data;
    }

    public function updates($data, $id=null)
    {
        $where = $id ? array('id' => $id) : null;
        $res = $this->validate(true)->save($data, $where);
        if( $res === false) {
            return $this->getError();
        } else {
            return $id ? $res : (int)$this->id;
        }
    }

    public function one($id)
    {
        $data = $this::get($id);
        if((!isset($data['is_delete']) || $data['is_delete'] == -1) || $data == null) {
            return 'notexist'; //数据已删除;
        } else {
            return $data->toArray();
        }
    }

    public function del($id)
    {
        return $this->save(array('is_delete'=> -1), array('id'=>$id)); //0 已删除  1删除
    }


    protected function getStatusAttr($value)
    {
        $status = [0=>'禁用',1=>'正常'];
        return $status[$value];
    }

    protected function getDelTextAttr($value,$data)
    {
        $status = [-1=>'删除',1=>'正常'];
        return $status[$data['is_delete']];
    }

    protected function getIsHideAttr($value)
    {
        $status = [1=>'显示',0=>'隐藏'];
        return $status[$value];
    }

}